﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Interop;
using System.Windows.Media;

namespace FizzGame
{
    class prtsc
    {
        Bitmap CaptureScreen()
        {
            /// new a bitmap with screen width and height
            Bitmap b = new Bitmap(
                (int)SystemParameters.PrimaryScreenWidth,
                (int)SystemParameters.PrimaryScreenHeight);

            /// copy screen through .net form api
            using (Graphics g = Graphics.FromImage(b))
            {
                g.CopyFromScreen(0, 0, 0, 0,
                    b.Size, CopyPixelOperation.SourceCopy);
            }

            return b;
        }

        [DllImport("gdi32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool DeleteObject(IntPtr hObject);

        private static BitmapSource ToBitmapSource(Bitmap b)
        {
            /// transform to hbitmap image
            IntPtr hb = b.GetHbitmap();

            /// convert hbitmap to bitmap source
            BitmapSource bs =
                Imaging.CreateBitmapSourceFromHBitmap(
                    hb, IntPtr.Zero, Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());

            /// release temp hbitmap image
            DeleteObject(hb);
            return bs;
        }

        public BitmapSource GetPrtSc()
        {
            return ToBitmapSource(CaptureScreen());
        }

        public BitmapSource GetGrayPrtSc()
        {
            return ToGray(ToBitmapSource(CaptureScreen()));
        }



        private FormatConvertedBitmap ToGray(BitmapSource source)
        {
            FormatConvertedBitmap bitmap = new FormatConvertedBitmap();
            bitmap.BeginInit();
            bitmap.Source = (BitmapSource)source;
            bitmap.DestinationFormat = PixelFormats.Gray32Float;
            bitmap.EndInit();
            return bitmap;
        }
    }
}
